<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="管理者ID" prop="manageId">
        <el-input
          v-model="queryParams.manageId"
          placeholder="请输入管理者ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管理者名称" prop="manageName">
        <el-input
          v-model="queryParams.manageName"
          placeholder="请输入管理者名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="支付类型" prop="payType">
       <el-select v-model="queryParams.payType" class="flex1" placeholder="请选择支付类型" style="width: 100%;">
          <el-option v-for="item in payTypeOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
       </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
       <el-select v-model="queryParams.orderType" class="flex1" placeholder="请选择订单类型" style="width: 100%;">
          <el-option v-for="item in orderTypeOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
       </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
       <el-select v-model="queryParams.orderStatus" class="flex1" placeholder="请选择订单状态" style="width: 100%;">
          <el-option v-for="item in orderStatusOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
       </el-select>
      </el-form-item>
      <el-form-item label="支付时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="dateChange"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

   <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['order:orderInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:orderInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:orderInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:orderInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <div style="margin-bottom: 10px;">
      <span style="color: red; font-weight:bold;">总额：{{totalAmount}}(元)</span>
    </div>
    <el-table v-loading="loading" :data="orderInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="订单类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <div v-if="scope.row.orderType == 1">充值VIP</div>
          <div v-if="scope.row.orderType == 6">充值金币</div>
        </template>
      </el-table-column>
      <el-table-column label="订单金额(元)" align="center" prop="orderAmount" />
      <el-table-column label="支付类型" align="center" prop="payType">
        <template slot-scope="scope">
          <div v-if="scope.row.payType == 1">微信</div>
          <div v-if="scope.row.payType == 2">支付宝</div>
          <div v-if="scope.row.payType == 3">金币支付</div>
          <div v-if="scope.row.payType == 4">苹果支付</div>
        </template>
      </el-table-column>

      <el-table-column label="订单创建时间" align="center" prop="orderCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderCreateTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="三方交易号" align="center" prop="tradeNo" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <div v-if="scope.row.orderStatus == 0">未支付</div>
          <div v-if="scope.row.orderStatus == 1">已支付</div>
          <div v-if="scope.row.orderStatus == 2">已完成</div>
          <div v-if="scope.row.orderStatus == 3">已退款</div>
          <div v-if="scope.row.orderStatus == 4">已取消</div>
          <div v-if="scope.row.orderStatus == 5">进行中</div>
          <div v-if="scope.row.orderStatus == 6">已过期</div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
         <!--  <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:orderInfo:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:orderInfo:remove1']"
          >删除</el-button>
          <el-button
             size="mini"
             type="text"
             icon="el-icon-circle-plus"
             @click="handleJoinManage(scope.row)"
             v-hasPermi="['order:orderInfo:join']"
           >加入旗下</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="关联ID(订单类型对应主体ID)" prop="relatedId">
          <el-input v-model="form.relatedId" placeholder="请输入关联ID(订单类型对应主体ID)" />
        </el-form-item>
        <el-form-item label="订单金额" prop="orderAmount">
          <el-input v-model="form.orderAmount" placeholder="请输入订单金额" />
        </el-form-item>
        <el-form-item label="实际金额" prop="actualAmount">
          <el-input v-model="form.actualAmount" placeholder="请输入实际金额" />
        </el-form-item>
        <el-form-item label="支付用户ID" prop="payUserId">
          <el-input v-model="form.payUserId" placeholder="请输入支付用户ID" />
        </el-form-item>
        <el-form-item label="订单创建时间" prop="orderCreateTime">
          <el-date-picker clearable
            v-model="form.orderCreateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择订单创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单更新时间" prop="orderUpdateTime">
          <el-date-picker clearable
            v-model="form.orderUpdateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择订单更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="微信交易号" prop="tradeNo">
          <el-input v-model="form.tradeNo" placeholder="请输入微信交易号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="对方ID 用户分润" prop="toUserId">
          <el-input v-model="form.toUserId" placeholder="请输入对方ID 用户分润" />
        </el-form-item>
        <el-form-item label="扩展字段" prop="extend">
          <el-input v-model="form.extend" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrderInfo, getOrderInfo, delOrderInfo, addOrderInfo, updateOrderInfo,joinManage } from "@/api/order/orderInfo";

export default {
  name: "OrderInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRange:'',
      totalAmount:'0',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        nickName: null,
        orderType: null,
        relatedId: null,
        orderAmount: null,
        actualAmount: null,
        payUserId: null,
        payType: null,
        orderCreateTime: null,
        orderUpdateTime: null,
        tradeNo: null,
        orderStatus: null,
        toUserId: null,
        extend: null,
        begTime: '',
        endTime: ''
      },
      // 表单参数
      form: {},
      // 1：充值VIP 2：单次咨询支付 3：打赏咨询 6：充值金币
      orderTypeOptions: [{
      	value: 1,
      	label: '充值VIP'
      },{
      	value: 6,
      	label: '充值花粉币'
      }],
      payTypeOptions: [{
      	value: 1,
      	label: '微信'
      }, {
      	value: 2,
      	label: '支付宝'
      }, {
      	value: 3,
      	label: '花粉币'
      },{
      	value: 4,
      	label: '苹果'
      }],
      // 订单状态(0:未支付1:已支付 2:已完成,3:已退款 4:已取消5:进行中6:已过期)
      orderStatusOptions: [{
      	value: 0,
      	label: '未支付'
      }, {
      	value: 1,
      	label: '已支付'
      }, {
      	value: 2,
      	label: '已完成'
      }, {
      	value: 3,
      	label: '已退款'
      }, {
      	value: 4,
      	label: '已取消'
      }],
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrderInfo(this.queryParams).then(response => {
        this.orderInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.totalAmount = response.extend;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        orderType: null,
        relatedId: null,
        orderAmount: null,
        actualAmount: null,
        payUserId: null,
        payType: null,
        orderCreateTime: null,
        orderUpdateTime: null,
        tradeNo: null,
        orderStatus: "0",
        createTime: null,
        updateTime: null,
        remark: null,
        toUserId: null,
        extend: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.begTime = '';
      this.queryParams.endTime = '';
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();


    },
    dateChange(e) {
      console.log(e)
      this.queryParams.begTime = e[0];
      this.queryParams.endTime = e[1];
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },

    // 加入旗下
    handleJoinManage(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否将该订单加入当前登录账号旗下？').then(function() {
        return joinManage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("加入成功");
      }).catch(() => {});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrderInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrderInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrderInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrderInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/orderInfo/export', {
        ...this.queryParams
      }, `orderInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
